/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.dao.impl;

import com.ivanbiz.dao.InternalDeliveryDAO;
import com.ivanbiz.helper.HibernateUtil;
import com.ivanbiz.model.InternalDelivery;
import com.ivanbiz.model.InternalDeliveryItem;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Administrator
 */
public class InternalDeliveryDAOImpl extends GenericDAOImpl implements InternalDeliveryDAO {

    @Override
    public List getDataByLike(String variable1, Object input1, String variable2, Object input2) throws Exception {
        try {
            Session session = HibernateUtil.getSession();
            Query queryOrder = session.createQuery("from com.ivanbiz.model.InternalDelivery po where po." + variable1 + " like '%" + input1 + "%' and po." + variable2 + " = '" + input2 + "' ");
            List list = queryOrder.list();
            return list;
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

    @Override
    public void delete(InternalDelivery internalDelivery) throws Exception {
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            Query queryPOI = session.createQuery("from com.ivanbiz.model.InternalDeliveryItem poi where poi.nomorInternalDelivery = '" + internalDelivery.getNomorInternalDelivery() + "'");
            if (!queryPOI.list().isEmpty()) {
                List listInternalDelivery = queryPOI.list();
                for (int x = 0; x < listInternalDelivery.size(); x++) {
                    InternalDeliveryItem internalDeliveryItem = (InternalDeliveryItem) listInternalDelivery.get(x);
                    session.delete(internalDeliveryItem);
                }
            }
            session.delete(internalDelivery);
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

    @Override
    public String getLastNoTransaksi() throws Exception {
        InternalDelivery internalDelivery = null;
        String noTransaksi = "";
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            Query query = session.createQuery("from com.ivanbiz.model.InternalDelivery p order by p.id desc");
            query.setFirstResult(0);
            query.setMaxResults(1);
            internalDelivery = (InternalDelivery) query.uniqueResult();
            if (internalDelivery != null) {
                noTransaksi = internalDelivery.getNomorInternalDelivery();
            }
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
        return noTransaksi;
    }
}
